﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace eDream.Page
{
    public partial class matkhau : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btn_Click(object sender, EventArgs e)
        {
            if (!txtMk0.Text.Equals("") && !txtMk1.Text.Equals("") && !txtMk2.Text.Equals("") && (txtMk1.Text.Trim() == txtMk2.Text.Trim()))
            {

                DataClasses1DataContext dx = new DataClasses1DataContext();
                try
                {
                    dx.Connection.Open();
                    int resul = checkPass(Global.ToEncoding(txtMk0.Text.Trim()));
                    if (resul > 0)
                    {
                        UserDB user_obj = (UserDB)Session[Global.SESS_USER];
                        UserDB u = new UserDB();
                        u = dx.UserDBs.Single(p => p.ID == user_obj.ID);
                        u.Password = Global.ToEncoding(txtMk1.Text.Trim());
                        dx.SubmitChanges();
                        lbThongbao.ForeColor = Color.Blue;
                        lbThongbao.Text = "Mật khẩu của bạn đã được thay đỗi";
                        txtMk0.Text = "";
                        txtMk1.Text = "";
                        txtMk2.Text = "";
                    }
                    else
                    {
                        lbThongbao.ForeColor = Color.Red;
                        lbThongbao.Text = "Mật khẩu cũ không đúng";

                    }
                }
                catch (Exception) { }
                finally { dx.Connection.Close(); }
            }

        }
        int checkPass(string text)
        {
            int resul = 0;
            UserDB user_obj = (UserDB)Session[Global.SESS_USER];
            DataClasses1DataContext dx = new DataClasses1DataContext();
            try
            {
                dx.Connection.Open();
                var list = from k in dx.UserDBs
                           where k.ID == user_obj.ID && k.Password == text
                           select k;
                foreach (UserDB u in list)
                {
                    resul = u.ID;
                    break;
                }
            }
            catch (Exception) { }
            finally { dx.Connection.Close(); }

            return resul;
        }
    }
}